package com.webchat.chat.core.tools;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * Created by Owner on 2018/6/27.
 */
public class SaltUtil {

    private SaltUtil() {
    }

    public static String getSalt(String s) {
        return getSalt(s, "");
    }

    public static String getSalt(String username, String password) {
        StringBuffer sb = null;
        sb = new StringBuffer();
        sb.append(username).append(password);
        return new Md5Hash(sb.toString(), RandomStringUtils.randomNumeric(6)).toHex();
    }

    public static String encodeMd5Hash(String pwd, String salt) {
        return new SimpleHash("md5", pwd, salt, 2).toHex();
    }


    public static final String PREFIX = "kunm";

    public static String encode(String str) {
        if (str == null) {
            return null;
        }
        return DigestUtils.md5Hex(PREFIX + str);
    }

    public static void main(String[] args) {

        System.out.println(encode("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36 Maxthon/5.1.7.2000y2017"));

    }
}
